﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using JohnnySins.DAL;
using JohnnySins.DAL.Repositories;
using JohnnySins.Entities;
using JohnnySins.Code;

namespace JohnnySins
{
    public partial class PornStars : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadAlphabet();
                LoadSearch();
            }
            pager.Attributes.Add("class", "pagination pagination-sm");
        }

        public void LoadAlphabet()
        {
            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                List<Alphabet> lst = repo.GetAlphabet(true, "first", "ALL");
                dlAlphabets.DataSource = lst;
                dlAlphabets.DataBind();
            }
        }

        public void LoadSearch()
        {
            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                int? rank = null;
                if (rbRankNew.Checked)
                    rank = 1;
                string letter = GetSelectedAlphabet();
                string gender = "ALL";
                if (rbGenderFemale.Checked)
                    gender = "female";
                else if (rbGenderMale.Checked)
                    gender = "male";
                int sortBy = 1;
                if (rbSortNumberScenes.Checked)
                    sortBy = 2;

                IEnumerable<StarProfileSearchResult> list = repo.SearchUI(sortBy, rank, gender, letter);
                rptScene.DataSource = list;
                rptScene.DataBind();

                lblCount.Text = list.Count().ToString();
            }
        }


        protected string GetSelectedAlphabet()
        {
            foreach (RepeaterItem ri in dlAlphabets.Items)
            {
                RadioButton rb = ((RadioButton)ri.FindControl("rbLetter"));
                if (rb.Checked)
                    return rb.Text;
            }

            return "ALL";
        }

        protected void btnApplyFilter_Click(object sender, EventArgs e)
        {
            LoadSearch();
        }

        protected void btnClearFilter_Click(object sender, EventArgs e)
        {
            rbGenderAll.Checked = true;
            rbRankNew.Checked = true;
            rbSortName.Checked = true;
            LoadAlphabet();
        }

        protected void rptScene_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

        }
        
    }
}